#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@gmail.com>
# CreateTime: 2019-01-07 10:13:30

import gevent
from gevent.pool import Pool
from gevent.lock import BoundedSemaphore


sem = BoundedSemaphore(1)


def worker1(n):
    sem.acquire()
    print('worker %d acquire sem' %n)
    gevent.sleep(0)
    sem.release()
    print('woker %d release sem' %n)


def worker2(n):
    with sem:
        print( ' worker %d acquire sem'  %n )
        gevent.sleep( 0 )
    print( ' woker %d release sem ' %n )


def worker3(n):
    print('worker %d acquire sem' %n)
    gevent.sleep(0)
    print('woker %d release sem' %n)


pool = Pool()
pool.map(worker2 , range(0, 5))
